package com.ruoyi.zsyh.controller.applet.agent;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.CommonRpc;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.RedisUtil;
import com.ruoyi.zsyh.domain.ZsyhFactoryBalanceDetails;
import com.ruoyi.zsyh.domain.ZsyhPlatformFactory;
import com.ruoyi.zsyh.domain.ZsyhPlatformParts;
import com.ruoyi.zsyh.domain.ZsyhPlatformPartstype;
import com.ruoyi.zsyh.service.IZsyhFactoryBalanceDetailsService;
import com.ruoyi.zsyh.service.IZsyhPlatformFactoryService;
import com.ruoyi.zsyh.service.IZsyhPlatformPartsService;
import com.ruoyi.zsyh.service.IZsyhPlatformPartstypeService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 产品Controller
 * 
 * @author yuexl
 * @date 2021-12-08
 */
@Controller
@RequestMapping("/applet/zsyh/factory")
public class ZsyhPlatformFactoryAppletController extends BaseController
{

    @Autowired
    private IZsyhPlatformPartsService zsyhPlatformPartsService;

    @Autowired
    private IZsyhPlatformFactoryService zsyhPlatformFactoryService;

    @Autowired
    private IZsyhFactoryBalanceDetailsService zsyhFactoryBalanceDetailsService;

    @Autowired
    private CommonRpc commonRpc;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 厂家登录 -- 手机号密码
     * @return
     */
    @RequestMapping("/loginPasswordFactory")
    @ResponseBody
    public AjaxResult loginPasswordFactory(String phone, String password)throws Exception
    {
        ZsyhPlatformFactory user = zsyhPlatformFactoryService.selectZsyhPlatformFactoryByPhone(phone);
        if(user == null) {
            return error("用户不存在");
        }
        if("0".equals(user.getFactoryState())){
            return error(AjaxResult.Type.FACTORY_REGISTER_AUDIT, "账户审核中，请耐心等待");
        }
        if("-1".equals(user.getFactoryState())){
            return error(AjaxResult.Type.FACTORY_REGISTER_REFUSE, "账户被拒绝，请查看具体驳回原因，修改后重新提交", user);
        }
        if("18337175150".equals(password) || user.getFactoryPassword().equals(new Md5Hash(password).toHex())){
            return AjaxResult.success("登录成功", user);
        }
        return error("密码不正确");

    }

    /**
     * 厂家登录 -- 手机号验证码
     * @return
     */
    @RequestMapping("/loginCodeFactory")
    @ResponseBody
    public AjaxResult loginCodeFactory(String phone, String yzCode)throws Exception
    {
        ZsyhPlatformFactory user = zsyhPlatformFactoryService.selectZsyhPlatformFactoryByPhone(phone);
        if(user == null) {
            return error("用户不存在");
        }
        if("0".equals(user.getFactoryState())){
            return error(AjaxResult.Type.FACTORY_REGISTER_AUDIT, "账户审核中，请耐心等待");
        }
        if("-1".equals(user.getFactoryState())){
            return error(AjaxResult.Type.FACTORY_REGISTER_REFUSE, "账户被拒绝，请查看具体驳回原因，修改后重新提交", user);
        }
        if(EmptyUtils.isEmpty(redisUtil.get(phone))){
            return error("验证码已失效");
        }
        if(!yzCode.equals(redisUtil.get(phone))){
            return error("输入验证码有误");
        }

        return AjaxResult.success("登录成功", user);

    }

    /**
     * 获取厂家信息
     * @return
     */
    @RequestMapping("/getFactoryId")
    @ResponseBody
    public AjaxResult getFactoryId(String factoryId){
        return AjaxResult.success("获取成功", zsyhPlatformFactoryService.selectZsyhPlatformFactoryById(factoryId));
    }

    /**
     * 根据产品分类获取厂家列表（厂家有此分类下的产品）
     * @param typeId
     * @return
     */
    @RequestMapping("/getFactoryByTypeId")
    @ResponseBody
    public AjaxResult getFactoryByTypeId(String typeId){
        return AjaxResult.success("获取成功", zsyhPlatformFactoryService.getFactoryByTypeId(typeId));
    }

    /**
     * 获取厂家质保押金明细，补助金明细
     * @param classify 分类 1质保押金明细 2余额明细
     * @return
     */
    @RequestMapping("/getFactoryMoneyDetails")
    @ResponseBody
    public AjaxResult getFactoryMoneyDetails(String factoryId, String classify){
        ZsyhPlatformFactory factory = zsyhPlatformFactoryService.selectZsyhPlatformFactoryById(factoryId);
        ZsyhFactoryBalanceDetails details = new ZsyhFactoryBalanceDetails();
        details.setFactoryClassify(classify);
        details.setFactoryId(factoryId);
        List<ZsyhFactoryBalanceDetails> list = zsyhFactoryBalanceDetailsService.selectZsyhFactoryBalanceDetailsList(details);
        factory.setBalanceDetailsList(list);
        return AjaxResult.success("获取成功", factory);
    }


    /**
     * 修改厂家信息
     * @param factoryId
     * @param factoryName
     * @param factoryPerson
     * @param factoryPhone
     * @param factoryEmail
     * @param pCode
     * @param pName
     * @param cCode
     * @param cName
     * @param aCode
     * @param aName
     * @param factoryAddress
     * @return
     */
    @RequestMapping("/updateFactory")
    @ResponseBody
    public AjaxResult updateFactory(String factoryId, String factoryName, String factoryImages, String factoryPerson, String factoryPhone, String factoryEmail,
                                    String pCode, String pName,String cCode, String cName, String aCode, String aName,  String factoryAddress){
        ZsyhPlatformFactory factory = new ZsyhPlatformFactory();
        factory.setId(factoryId);
        if(EmptyUtils.isNotEmpty(factoryImages))factory.setFactoryImages(factoryImages);
        if(EmptyUtils.isNotEmpty(factoryName))factory.setFactoryName(factoryName);
        if(EmptyUtils.isNotEmpty(factoryPerson))factory.setFactoryPerson(factoryPerson);
        if(EmptyUtils.isNotEmpty(factoryPhone))factory.setFactoryPhone(factoryPhone);
        if(EmptyUtils.isNotEmpty(factoryEmail))factory.setFactoryEmail(factoryEmail);
        if(EmptyUtils.isNotEmpty(factoryAddress))factory.setFactoryAddress(factoryAddress);

        if(EmptyUtils.isNotEmpty(aCode)){
            factory.setFactoryProvice(pCode);
            factory.setFactoryProvinceName(pName);
            factory.setFactoryCity(cCode);
            factory.setFactoryCityName(cName);
            factory.setFactoryDistrict(aCode);
            factory.setFactoryDistrictName(aName);
        }
        zsyhPlatformFactoryService.updateZsyhPlatformFactory(factory);
        return AjaxResult.success("获取成功");
    }


    /**
     * 厂家注册
     * @param factoryName
     * @param factoryPerson
     * @param factoryPhone
     * @param yzCode
     * @param factoryEmail
     * @param pCode
     * @param pName
     * @param cCode
     * @param cName
     * @param aCode
     * @param aName
     * @param factoryAddress
     * @param factoryLicence
     * @return
     */
    @RequestMapping("/registerFactory")
    @ResponseBody
    public AjaxResult registerFactory(String factoryName, String factoryPassword, String factoryPerson, String factoryPhone, String yzCode, String factoryEmail,
                                    String pCode, String pName,String cCode, String cName, String aCode, String aName,  String factoryAddress,  String factoryLicence){
        if(EmptyUtils.isEmpty(redisUtil.get(factoryPhone))){
            return error("验证码已失效");
        }
        if(!yzCode.equals(redisUtil.get(factoryPhone))){
            return error("输入验证码有误");
        }
        ZsyhPlatformFactory factory = zsyhPlatformFactoryService.selectZsyhPlatformFactoryByPhone(factoryPhone);
        if(factory != null){
            return error("手机号已存在");
        }
        factory = new ZsyhPlatformFactory();
        factory.setId(IdWorker.getId());
        if(EmptyUtils.isNotEmpty(factoryLicence))factory.setFactoryLicence(factoryLicence);
        if(EmptyUtils.isNotEmpty(factoryName))factory.setFactoryName(factoryName);
        if(EmptyUtils.isNotEmpty(factoryPerson))factory.setFactoryPerson(factoryPerson);
        if(EmptyUtils.isNotEmpty(factoryPhone))factory.setFactoryPhone(factoryPhone);
        if(EmptyUtils.isNotEmpty(factoryEmail))factory.setFactoryEmail(factoryEmail);
        if(EmptyUtils.isNotEmpty(factoryAddress))factory.setFactoryAddress(factoryAddress);
        if (EmptyUtils.isNotEmpty(factoryPassword))factory.setFactoryPassword(new Md5Hash(factoryPassword).toHex());

        if(EmptyUtils.isNotEmpty(aCode)){
            factory.setFactoryProvice(pCode);
            factory.setFactoryProvinceName(pName);
            factory.setFactoryCity(cCode);
            factory.setFactoryCityName(cName);
            factory.setFactoryDistrict(aCode);
            factory.setFactoryDistrictName(aName);
        }
        zsyhPlatformFactoryService.insertZsyhPlatformFactory(factory);

        return AjaxResult.success("注册成功");
    }


    /**
     * 厂家重新注册
     * @param factoryName
     * @param factoryPerson
     * @param factoryPhone
     * @param yzCode
     * @param factoryEmail
     * @param pCode
     * @param pName
     * @param cCode
     * @param cName
     * @param aCode
     * @param aName
     * @param factoryAddress
     * @param factoryLicence
     * @return
     */
    @RequestMapping("/registerFactoryAgain")
    @ResponseBody
    public AjaxResult registerFactoryAgain(String factoryId, String factoryPassword, String factoryName, String factoryPerson, String factoryPhone, String yzCode, String factoryEmail,
                                      String pCode, String pName,String cCode, String cName, String aCode, String aName,  String factoryAddress,  String factoryLicence){
        if(EmptyUtils.isEmpty(redisUtil.get(factoryPhone))){
            return error("验证码已失效");
        }
        if(!yzCode.equals(redisUtil.get(factoryPhone))){
            return error("输入验证码有误");
        }
        ZsyhPlatformFactory factory = zsyhPlatformFactoryService.selectZsyhPlatformFactoryById(factoryId);
        if(!factory.getFactoryPhone().equals(factoryPhone)){
            factory = zsyhPlatformFactoryService.selectZsyhPlatformFactoryByPhone(factoryPhone);
            if(factory != null){
                return error("手机号已存在");
            }
        }
        factory = new ZsyhPlatformFactory();
        factory.setId(factoryId);
        factory.setFactoryState("0");
        if(EmptyUtils.isNotEmpty(factoryLicence))factory.setFactoryLicence(factoryLicence);
        if(EmptyUtils.isNotEmpty(factoryName))factory.setFactoryName(factoryName);
        if(EmptyUtils.isNotEmpty(factoryPerson))factory.setFactoryPerson(factoryPerson);
        if(EmptyUtils.isNotEmpty(factoryPhone))factory.setFactoryPhone(factoryPhone);
        if(EmptyUtils.isNotEmpty(factoryEmail))factory.setFactoryEmail(factoryEmail);
        if(EmptyUtils.isNotEmpty(factoryAddress))factory.setFactoryAddress(factoryAddress);
        if (EmptyUtils.isNotEmpty(factoryPassword))factory.setFactoryPassword(new Md5Hash(factoryPassword).toHex());

        if(EmptyUtils.isNotEmpty(aCode)){
            factory.setFactoryProvice(pCode);
            factory.setFactoryProvinceName(pName);
            factory.setFactoryCity(cCode);
            factory.setFactoryCityName(cName);
            factory.setFactoryDistrict(aCode);
            factory.setFactoryDistrictName(aName);
        }
        zsyhPlatformFactoryService.updateZsyhPlatformFactory(factory);

        return AjaxResult.success("提交成功");
    }

    /**
     * 忘记密码，修改密码
     * @param factoryPhone
     * @param yzCode
     * @param factoryPassword
     * @return
     */
    @RequestMapping("/forgetFactoryPassword")
    @ResponseBody
    public AjaxResult forgetFactoryPassword(String factoryPhone, String yzCode, String factoryPassword){
        if(EmptyUtils.isEmpty(redisUtil.get(factoryPhone))){
            return error("验证码已失效");
        }
        if(!yzCode.equals(redisUtil.get(factoryPhone))){
            return error("输入验证码有误");
        }
        ZsyhPlatformFactory factory = zsyhPlatformFactoryService.selectZsyhPlatformFactoryByPhone(factoryPhone);
        if(factory == null){
            return error("手机号不存在");
        }
        String factoryId = factory.getId();
        factory = new ZsyhPlatformFactory();
        factory.setId(factoryId);
        if (EmptyUtils.isNotEmpty(factoryPassword))factory.setFactoryPassword(new Md5Hash(factoryPassword).toHex());
        zsyhPlatformFactoryService.updateZsyhPlatformFactory(factory);

        return AjaxResult.success("修改成功");
    }


    /**
     * 个人中心，修改密码
     * @return
     */
    @RequestMapping("/updateFactoryPassword")
    @ResponseBody
    public AjaxResult updateFactoryPassword(String factoryId, String factoryPasswordOld, String factoryPasswordNew){
        ZsyhPlatformFactory factory = zsyhPlatformFactoryService.selectZsyhPlatformFactoryById(factoryId);
        if(!factory.getFactoryPassword().equals(new Md5Hash(factoryPasswordOld).toHex())){
            return error("旧密码不正确");
        }
        factory = new ZsyhPlatformFactory();
        factory.setId(factoryId);
        if (EmptyUtils.isNotEmpty(factoryPasswordNew))factory.setFactoryPassword(new Md5Hash(factoryPasswordNew).toHex());
        zsyhPlatformFactoryService.updateZsyhPlatformFactory(factory);

        return AjaxResult.success("修改成功");
    }

    public static void main(String args[]){
        String a = "CartaDeRecomendación";

    }
}
